In [6]:
cd ..
In [7]:
%env THEANO_FLAGS = device=gpu3,floatX=float32,base_compiledir=~/.theano/stonesoup3
In [8]:
import pylearn2.utils
import pylearn2.config
import theano
import neukrill_net.utils
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import holoviews as hl
%load_ext holoviews.ipython
import sklearn.metrics
import pickle
import neukrill_net.utils
import neukrill_net.encoding as enc
import neukrill_net.taxonomy as t
In [9]:
settings = neukrill_net.utils.Settings("settings.json")
run_settings = neukrill_net.utils.load_run_settings('run_settings/experiment_setting_colnorms_higher_aug.json',
settings, force=True)
model = pylearn2.utils.serial.load(run_settings['pickle abspath'])
In [10]:
# format the YAML
yaml_string = neukrill_net.utils.format_yaml(run_settings, settings)
# load proxied objects
proxied = pylearn2.config.yaml_parse.load(yaml_string, instantiate=False)
# pull out proxied dataset
proxdata = proxied.keywords['dataset']
# force loading of dataset and switch to test dataset
proxdata.keywords['force'] = True
proxdata.keywords['training_set_mode'] = 'test'
proxdata.keywords['verbose'] = False
# then instantiate the dataset
dataset = pylearn2.config.yaml_parse._instantiate(proxdata)
In [11]:
if hasattr(dataset.X, 'shape'):
N_examples = dataset.X.shape[0]
else:
N_examples = len(dataset.X)
batch_size = 500
while N_examples%batch_size != 0:
batch_size += 1
n_batches = int(N_examples/batch_size)
#n_classes = len(settings.classes)
In [12]:
model.set_batch_size(batch_size)
X = model.get_input_space().make_batch_theano()
Y = model.fprop(X)
if type(X) == tuple:
f = theano.function(X,Y)
else:
f = theano.function([X],Y)
In [13]:
augment = 1
y = np.zeros((N_examples*augment,188))
# get the data specs from the cost function using the model
pcost = proxied.keywords['algorithm'].keywords['cost']
cost = pylearn2.config.yaml_parse._instantiate(pcost)
data_specs = cost.get_data_specs(model)
i = 0
for _ in range(augment):
# make sequential iterator
iterator = dataset.iterator(batch_size=batch_size,num_batches=n_batches,
mode='even_sequential', data_specs=data_specs)
for batch in iterator:
if type(X) == tuple:
y[i*batch_size:(i+1)*batch_size,:] = f(batch[0],batch[1])
else:
y[i*batch_size:(i+1)*batch_size,:] = f(batch[0])
i += 1
af = run_settings.get("augmentation_factor",1)
if af > 1:
y_collapsed = np.zeros((int(N_examples/af), 188))
for i,(low,high) in enumerate(zip(range(0,dataset.y.shape[0],af),
range(af,dataset.y.shape[0]+af,af))):
y_collapsed[i,:] = np.mean(y[low:high,:], axis=0)
y = y_collapsed
# and collapse labels
labels = dataset.y[range(0,dataset.y.shape[0],af)]
elif augment > 1:
y_collapsed = np.zeros((N_examples,188))
# different kind of augmentation, has to be collapsed differently
for row in range(N_examples):
y_collapsed[row,:] = np.mean(np.vstack([r for r in
y[[i for i in range(row,N_examples*augment,N_examples)],:]]),
axis=0)
y = y_collapsed
labels = dataset.y
else:
labels = dataset.y
Get y.
In [14]:
y.shape
Out[14]:
Copy it into predictions, make class_predictions and superclass predictions (first parent).
In [15]:
predictions = np.zeros(y.shape)
np.copyto(predictions, y)
predictions.shape
Out[15]:
In [17]:
class_predictions = np.zeros((y.shape[0], 121))
np.copyto(class_predictions, predictions[:, :121])
class_predictions.shape
Out[17]:
In [18]:
superclasses = np.zeros((y.shape[0], 38))
np.copyto(superclasses, predictions[:,121:(121+38)])
superclasses.shape
Out[18]:
In [19]:
layer = t.TaxonomyLayer(1)
hier = enc.get_hierarchy(settings)
priors = np.zeros(len(hier[1]))
#superclass_children = [[] for k in range(len(hier[1]))]
# For each class
for i, c in enumerate(settings.classes):
# Find index of its first parent in the 1-of-k encoding array
j = int(np.where(np.array(hier[1]) == layer[c])[0])
# Add class's prior for that superclass group
priors[j] += settings.class_priors[i]
# Record index of this class as a child or superclass
#superclass_children[j].append(i)
In [20]:
priors
Out[20]:
In [21]:
new_class_predictions = np.zeros(class_predictions.shape)
new_class_predictions.shape
Out[21]:
In [22]:
for index, row in enumerate(class_predictions):
for i, c in enumerate(settings.classes):
j = int(np.where(np.array(hier[1]) == layer[c])[0])
new_class_predictions[index, i] = superclasses[index, j] * (settings.class_priors[i] / priors[j])
Try with different weights.
In [27]:
labels = labels[:, :121]
results = []
weights = np.logspace(-5,-1, 20)
for weight in weights:
updated_predictions = np.zeros(class_predictions.shape)
updated_predictions = class_predictions * (1-weight) + weight * new_class_predictions
logloss = sklearn.metrics.log_loss(labels, updated_predictions)
results.append(logloss)
In [28]:
results
Out[28]:
In [29]:
plt.semilogx(weights, results)
Out[29]:
In [32]:
class_predictions.shape
Out[32]:
Now process all vectors. Make a big list with predictions for each vector.
In [33]:
all_predictions = [[] for k in range(len(hier))]
start = 0
for i in range(len(hier)):
end = len(hier[i])
all_predictions[i] = predictions[:, start:(start + end)]
start = start + end
Check the dimensions are right.
In [34]:
[z.shape for z in all_predictions]
Out[34]:
Make a list for priors for all vectors. Set it with class priors in the zeroth vector.
In [36]:
priors = [[] for k in range(len(hier))]
for i in range(len(hier)):
priors[i] = np.zeros(len(hier[i]))
priors[0] = settings.class_priors
In [37]:
priors
Out[37]:
Propagate up to compute superclass group priors in all vectors.
In [38]:
# For each class
for index, h in enumerate(hier):
if (index < 5):
layer = t.TaxonomyLayer(index + 1)
for i, c in enumerate(settings.classes):
# Find index of its parent in 1-of-k encodings
j = int(np.where(np.array(hier[index + 1]) == layer[c])[0])
# Add class's prior for that superclass group
priors[index + 1][j] += settings.class_priors[i]
In [39]:
priors
Out[39]:
Check they sum to 1.
In [40]:
[sum(q) for q in priors]
Out[40]:
Make a big list for all new predictions.
In [41]:
new_predictions = [np.zeros(class_predictions.shape) for k in range(len(hier))]
new_predictions[0] = class_predictions
In [42]:
new_predictions
Out[42]:
In [43]:
# For each array of new predictions
for pred in range(len(new_predictions)):
# No need to update the zeroth one
if pred > 0:
layer = t.TaxonomyLayer(pred)
# For each image
for index, row in enumerate(new_predictions[pred]):
# For each class
for i, c in enumerate(settings.classes):
# Find the parent
j = int(np.where(np.array(hier[pred]) == layer[c])[0])
new_predictions[pred][index, i] = all_predictions[pred][index, j] * (settings.class_priors[i] / priors[pred][j])
In [44]:
new_predictions
Out[44]: